package com.lh0811.forge.dependency.basic_web.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
@Schema(description = "统一响应")
public class ServerResponse<T> implements Serializable {

    @Schema(description = "请求结果状态0成功 其他是失败")
    private int status;

    @Schema(description = "附加消息")
    private String msg = "操作成功";

    @Schema(description = "数据")
    private T data;

    public ServerResponse() {
        super();
    }

    public ServerResponse(int status) {
        this.status = status;
    }

    public ServerResponse(int status, T data) {
        this.status = status;
        this.data = data;
    }

    public ServerResponse(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ServerResponse(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public boolean isSuccess() {
        return this.status == ResponseStatus.SUCCESS.getCode();
    }

    public Integer getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public static <T> ServerResponse<T> createBySuccess() {
        return new ServerResponse<T>(ResponseStatus.SUCCESS.getCode(), "操作成功");
    }

    public static <T> ServerResponse<T> createBySuccess(T data) {
        return new ServerResponse<T>(ResponseStatus.SUCCESS.getCode(), data);
    }

    public static <T> ServerResponse<T> createBySuccess(String msg) {
        return new ServerResponse<T>(ResponseStatus.SUCCESS.getCode(), msg);
    }

    public static <T> ServerResponse<T> createBySuccess(String msg, T data) {
        return new ServerResponse<T>(ResponseStatus.SUCCESS.getCode(), msg, data);
    }


    public static <T> ServerResponse<T> createByError() {
        return new ServerResponse<T>(ResponseStatus.ERROR.getCode(), ResponseStatus.ERROR.getDesc());
    }

    public static <T> ServerResponse<T> createByError(T data) {
        return new ServerResponse<T>(ResponseStatus.ERROR.getCode(), data);
    }

    public static <T> ServerResponse<T> createByError(String errorMessage) {
        return new ServerResponse<T>(ResponseStatus.ERROR.getCode(), errorMessage);
    }

    public static <T> ServerResponse<T> createByError(Integer errorCode, String errorMessage) {
        return new ServerResponse<T>(errorCode, errorMessage);
    }

    public static <T> ServerResponse<T> createByError(Integer code, String errorMessage, T data) {
        return new ServerResponse<T>(code, errorMessage, data);
    }

    public static <T> ServerResponse<T> createByError(String errorMessage, T data) {
        return new ServerResponse<T>(ResponseStatus.ERROR.getCode(), errorMessage, data);
    }


}
